package com.qiku.course.controller;

import com.qiku.course.entity.Course;
import com.qiku.course.entity.CourseDTO;
import com.qiku.course.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/course")
@CrossOrigin
public class CourseController {

    @Autowired
    private CourseService courseService;


    @GetMapping("/getAllCourse")
    public List<CourseDTO> getAllCourse(){
        List<CourseDTO> courseList = courseService.getAllCourse();
        return courseList;
    }


    @GetMapping("/getAllCourse2")
    public List<CourseDTO> getAllCourse2(){
        // 模拟高并发

        // 创建线程池 20个线程  创建线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(20);
        for (int i = 0; i < 20; i++) {
            threadPool.submit(new Runnable() {
                @Override
                public void run() {
                    courseService.getAllCourse();
                }
            });
        }
        List<CourseDTO> courseList = courseService.getAllCourse();
        return courseList;
    }


    @GetMapping("/getCoursesByUserId")
    public List<CourseDTO> getCoursesByUserId(@RequestParam("userid") Integer userId){
        List<CourseDTO> courseByUserId = courseService.getCourseByUserId(userId);
        return courseByUserId;
    }


    @GetMapping("/getCourseById/{courseId}")
    public CourseDTO getCourseById(@PathVariable("courseId") Integer courseId){
        CourseDTO courseById = courseService.getCourseById(courseId);
        return courseById;
    }

}
